/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.cache.CacheFactory;
import com.sinodata.bsm.cicp.cache.CollectFieldCache;
import com.sinodata.bsm.cicp.cache.CollectParamCache;
import com.sinodata.bsm.cicp.cache.CollectScheduleCache;
import com.sinodata.bsm.cicp.cache.CollectTaskCache;
import com.sinodata.bsm.cicp.cache.CollectorCache;
import com.sinodata.bsm.cicp.cache.ResCache;
import com.sinodata.bsm.cicp.task.TaskManager;
import com.sinodata.bsm.common.vo.CollectField;
import com.sinodata.bsm.common.vo.CollectParam;
import com.sinodata.bsm.common.vo.CollectSchedule;
import com.sinodata.bsm.common.vo.CollectTask;
import com.sinodata.bsm.common.vo.Collector;
import com.sinodata.bsm.common.vo.Res;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-22 上午10:25:28          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class CollectorService {
    private static Logger logger = Logger.getLogger(CollectorService.class);

    private final ResCache resCache = (ResCache) CacheFactory.getCache(ResCache.class);
    private final CollectTaskCache collectTaskCache = (CollectTaskCache) CacheFactory.getCache(CollectTaskCache.class);
    private final CollectParamCache collectParamCache = (CollectParamCache) CacheFactory.getCache(CollectParamCache.class);
    private final CollectFieldCache collectFieldCache = (CollectFieldCache) CacheFactory.getCache(CollectFieldCache.class);
    private final CollectScheduleCache collectScheduleCache = (CollectScheduleCache) CacheFactory.getCache(CollectScheduleCache.class);
    private final CollectorCache collectorCache = (CollectorCache) CacheFactory.getCache(CollectorCache.class);

    /**
     * 
     */
    public CollectorService() {
    }

    /**
     * 获取一个资源的采集任务，包括继承的模板和自定义任务，如果自定义任务覆盖了模板任务，则不包括覆盖的模板任务
     * @param res
     * @return
     */
    public List<CollectTask> getResCollectTasks(Res res) {
        List<CollectTask> resCollectTasks = new ArrayList<CollectTask>();
        List<CollectTask> tasksByResId = collectTaskCache.getByResId(res.getId());
        Set<Long> compSet = new HashSet<Long>();
        if (tasksByResId != null) {
            resCollectTasks.addAll(tasksByResId);
            for (CollectTask t : tasksByResId) {
                compSet.add(t.getResTypeTaskId());
            }
        }
        List<CollectTask> tasksByResTypeId = collectTaskCache.getByResTypeId(res.getResTypeId());
        if (tasksByResTypeId != null) {
            for (CollectTask t : tasksByResTypeId) {
                if (!compSet.contains(t.getId())) {
                    resCollectTasks.add(t);
                }
            }
        }
        return resCollectTasks;
    }

    public List<CollectSchedule> getCollectTaskSchedules(Long taskId) {
        return collectScheduleCache.getTaskSchedules(taskId);
    }

    public CollectTask getCollectTask(Long taskId) {
        return collectTaskCache.get(taskId);
    }

    public Collector getCollector(Long id) {
        return collectorCache.get(id);
    }

    public void collect(ArrayList<Long> resIds) {
        for (Long resId : resIds) {
            Res res = resCache.get(resId);
            TaskManager.getInstance().addTemporaryResTasks(res);
        }
    }

    /**
     * 新增资源自定义采集规则
     * @param task
     * @param scheduleList
     * @param paramList
     * @param fieldList
     * @throws Exception
     */
    public void addCollectTask(CollectTask task, ArrayList<CollectSchedule> scheduleList, ArrayList<CollectParam> paramList, ArrayList<CollectField> fieldList) throws Exception {
        CollectTask oldTask = collectTaskCache.get(task.getId().longValue());
        if (oldTask != null) {
            deleteCollectTask(oldTask.getId());
        }
        collectTaskCache.put(task);
        collectScheduleCache.put(task.getId(), scheduleList);
        collectParamCache.put(task.getId(), paramList);
        collectFieldCache.put(task.getId(), fieldList);
        Long resId = task.getResId();
        if (resId != null) {
            TaskManager.getInstance().restartResTask(resId);
        }
    }

    /**
     * 删除一个自定义资源采集规则
     * @param taskId
     */
    public void deleteCollectTask(Long taskId) {
        CollectTask task = collectTaskCache.get(taskId.longValue());
        Long resId = task.getResId();
        if (task.getResTypeId() != null || resId == null) {
            logger.error(">>>>>>>> can not deleteCollectTask id:" + taskId);
            return;
        }
        collectScheduleCache.removeByCollectTaskId(taskId);
        collectParamCache.removeCollectParamsByTaskId(taskId);
        collectFieldCache.removeCollectFieldsByTaskId(taskId);
        collectTaskCache.remove(taskId.longValue());
        TaskManager.getInstance().restartResTask(resId);
    }

    /**
     * 暂停或恢复资源采集任务
     * @param taskId
     */
    public void setCollectTaskPausedOrNormal(Long taskId, Integer newStatus) {
        CollectTask task = collectTaskCache.get(taskId.longValue());
        if (task.getResTypeId() != null || task.getResId() == null) {
            logger.error(">>>>>>>> can not setCollectTaskPaused id:" + taskId);
            return;
        }
        task.setStatus(newStatus);
        TaskManager.getInstance().restartResTask(task.getResId());
    }

}
